<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Rag Hu
 * Created Time: 2019/7/23 14:06
 * Modified Time: 2019/7/23 14:06
 * Version: 2.0
 */

namespace App\Modules\System\Services;


use App\Exceptions\AllValidatorException;
use App\Lib\File\AliOSS;
use App\Lib\File\AliOssImg;
use App\Models\Picture;
use Flower\Core\Repository\Eloquent\BaseService;
use Intervention\Image\AbstractFont;
use Intervention\Image\Facades\Image;

class ImageService extends BaseService
{
    public function upload($requestData){
        $rules = [
            'file' => 'required|file|image'
        ];
        $message = [
            'file.required' => '请上传图片',
            'file.image' => '请上传正确格式的图片'
        ];
        $validator = \Validator::make($requestData,$rules,$message);

        if(!$validator->passes()) {//通过表单验证
            $msg = array_values($validator->getMessageBag()->getMessages())[0];
            throw new AllValidatorException(['msg'=>$msg,'statusCode'=>400,'errorCode'=>400]);
        }


        $file = $requestData['file'];

        $oss = new AliOssImg();
        $img = $oss->upload($file,'images/'.date('Y-m-d',time()));
       // $ali_oss = new AliOSS();
       // $img = $ali_oss->upload($img,'test');
        return $img;
    }

    public function getShareImage()
    {
        $img = Image::make(base_path() . '/public/phone/invite_img.png');
        $img->text(121212, 350, 905, function (AbstractFont $font) {
            $font->file(base_path() . '/public/phone/a.TTF');
            $font->size(66);
            $font->color('#07befa');
        });
        return $img->response();
    }

    public function addByOSSUrl($oss_url)
    {
        $new_image         = new Picture;
        $new_image->name   = '图片';
        $new_image->image    = $oss_url;
        $new_image->status = 1;
        $new_image->save();
        return $new_image;
    }
}